let db = wx.cloud.database({
  env: 'mini-cloud-8gonte8a94b9dcfb'
});
let poemsl = db.collection('db_poemsl');
let author = db.collection('db_authors');
Page({

  data: {
    searchValue:"",
    arr:[],
    sta:false,
    history:[],
    author:[],
    hotsearch:[{text:"诗文"},{text:"咏物"},{text:"梅花"},{text:"小学文言文"},{text:"山水"}]
  },
  //点击热门搜索
  hotSearch(event){
       wx.showLoading({
            title: "正在加载...",
            icon: "none",
          });
    let type=event.currentTarget.dataset.name
    poemsl.where({type}).get().then(res=>{
      let {data,errMsg}=res
      if(errMsg=="collection.get:ok"){
        this.setData({
          arr:data,
          sta:true,
          author:[]
        })
        wx.hideLoading();
      }
    })
  },
  //点击历史记录查询数据
  searchVal(event){
    wx.showLoading({
        title: "正在加载...",
        icon: "none",
  });
    let val=event.currentTarget.dataset.val
    poemsl.where({
      name: {
        $regex: '.*' + val,
        $options: 'i'
      }
    }).get().then(res=>{
      this.setData({
        arr:res.data,
      })
    })

    //诗人
    author.where({
      name: {
        $regex: '.*' + val,
        $options: 'i'
      }
    }).get().then(res=>{
      this.setData({
        author:res.data,
        sta:true
      })
      wx.hideLoading();
    })
  },
  //获取input的值
  searchInput(event){
    this.setData({
      searchValue:event.detail.value.trim()
    })
  },
  //本地储存
  getLocal(val){
    let history=wx.getStorageSync("history")
    if(history.length==0){
    let arrs=[]
    arrs.push(val)
    wx.setStorageSync("history",arrs)
    let history=wx.getStorageSync("history")
    this.setData({
      history
    })
    }else{
      let hist=wx.getStorageSync("history")
      for(let i=0;i<hist.length;i++){
        if(hist[i]==val){
          return
        }else{
          hist.push(val)
      wx.setStorageSync("history",hist)
      let history=wx.getStorageSync("history")
      this.setData({
        history
      })
      return
    }
  }
      
    }
  },
  //删除历史记录
  deleteHistory(){
    wx.removeStorageSync("history")
    let history=wx.getStorageSync("history")
    this.setData({
      history
    })

  },
  //跳转诗词详情
  tiao(event){
    let id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: `../detail/detail?id=${id}`,
    })
  },
  //跳转诗人详情
  detail(event){
    let name=event.currentTarget.dataset.name
    wx.navigateTo({
      url: `../authordetail/authordetail?name=${name}`,
    })
  },
  //点击查询
  searchPoem(){
    if(this.data.searchValue.length==0){
      wx.showToast({
        title: '内容不能为空',
        icon:'none'
      })
      return
    }
      wx.showLoading({
              title: "正在加载...",
              icon: "none",
            });
      let val=this.data.searchValue
      poemsl.where({
        name: {
          $regex: '.*' + val,
          $options: 'i'
        }
      }).get().then(res=>{
        this.setData({
          arr:res.data,
          sta:true
        })
        this.getLocal(val);
        wx.hideLoading();
      })
    // }
   
  },
  onLoad(options) {
    let history=wx.getStorageSync("history")
    this.setData({
      history
    })

  },

  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})